<?php
/**
 * Created by PhpStorm.
 * User: xiaosong
 * Date: 2020-02-10
 * Time: 13:24
 */

namespace app\admin\controller;


use app\common\service\App;
use app\common\service\AppService;
use think\Config;
use think\Db;

class Im extends Base
{


    public function index()
    {

        $map = [];
        $map['msg_type'] = ['in',['RC:TxtMsg']] ;
        if (input('date')){

        }else{
            $pre = date('Y_').intval(date('m'));
        }

        $conn = Db::connect('db2')
            ->table('xs_messages_'.$pre);

        if (input('uuid')){
            if (!is_numeric(input('uuid'))){
                $uuid = dehashid(input('uuid'));
            }else{
                $uuid = input('uuid');
            }
            if (!is_numeric($uuid)){
                $this->error("ID错误!");
            }

            $where['user_id|uuid'] = input('uuid');

            $uid = Db::name('users')->where($where)->value('user_id');

            if (input('to_user')){

//                $map['from_user_id'] = hashid($uid);
//
                if (is_numeric(input('to_user'))){
                    $to_user =  hashid(input('to_user'));
                }else{
                    $to_user =  input('to_user');
                }
//                $map['to_user_id'] = $to_user;

                $map1['from_user_id'] = hashid($uid);
                $map1['to_user_id'] = $to_user;
                $map1['msg_type'] = $map['msg_type'];
                $or['from_user_id'] = $to_user;
                $or['to_user_id'] = hashid($uid);
                $or['msg_type'] = $map['msg_type'];
                $conn->where(function ($query)use ($map1){
                    $query->where($map1);

                })->whereOr(function ($query)use ($or){
                    $query->where($or);
                });;

                $map = [];
            }else{
                $map['from_user_id|to_user_id'] = ['in',hashid($uid)];
            }

        }


//        Db::name()->partition()

//        $rows = \app\common\model\Im::paginate();
//        dd($rows);
//
//        dd('messages_'.$pre);

        $rows = $conn
            ->where($map)
            ->order('id desc')
            ->paginate('', false, ['query' => request()->param()])->each(function ($arr){

                if (is_string($arr['content'])){
                    $arr['content'] = json_decode($arr['content'],true);
                    if (isset($arr['content']['user'])){
                        $arr['formInfo'] = $arr['content']['user'];
                    }
                    $arr['targetInfo'] = $arr['content']['user'];

                }

                switch ($arr['from_user_id']){
                    case "system":
                        $arr['formInfo'] = [
                            'user_id'    => 'system',
                            'nick_name'  => "<span style=\"color: red\">系统管理员</span>",
                            'header_img' => App::extend('sys_header_img'),
                        ];
                        break;
                    case 'sysCustomer':
                        $arr['formInfo'] = [
                            'user_id'    => 'sysCustomer',
                            'nick_name'  => "<span style=\"color: red\">小助手</span>",
                            'header_img' => App::extend('sys_header_img')
                        ];
                        break;
                    default:
                        if (starts_with($arr['from_user_id'],"admin")){ //客服信息查询
                            $user_id = dehashid(str_replace('admin','',$arr['from_user_id']));
                            if (is_numeric($user_id)){
                                $map['user_id'] = $user_id;
                                $map['is_service'] = 1;
                                $arr['formInfo'] = Db::name('admins')->where($map)->field('nick_name,header_img')->cache(3)->find();
                                $arr['user_id']  = $arr['from_user_id'];
                            }
                        }else{
                            $user_id = dehashid($arr['from_user_id']);
                            $arr['formInfo'] = AppService::getUserInfo('user_id,nick_name,header_img',$user_id);
                        }
                        break;
                }

                switch ($arr['to_user_id']){
                    case "system":
                        $arr['targetInfo'] = [
                            'user_id'    => 'system',
                            'nick_name'  => "<span style=\"color: red\">系统管理员</span>",
                            'header_img' => App::extend('sys_header_img'),
                        ];
                        break;
                    case 'sysCustomer':
                        $arr['targetInfo'] = [
                            'user_id'    => 'sysCustomer',
                            'nick_name'  => "<span style=\"color: red\">小助手</span>",
                            'header_img' => App::extend('sys_header_img')
                        ];
                        break;
                    default:
                        if (starts_with($arr['to_user_id'],"admin")){ //客服信息查询
                            $user_id = dehashid(str_replace('admin','',$arr['to_user_id']));
                            if (is_numeric($user_id)){
                                $map['user_id'] = $user_id;
                                $map['is_service'] = 1;
                                $arr['targetInfo'] = Db::name('admins')->where($map)->field('nick_name,header_img')->cache(3)->find();
                                $arr['user_id'] = $arr['to_user_id'];
                            }
                        }else{
                            $user_id = dehashid($arr['to_user_id']);

                            $arr['targetInfo'] = AppService::getUserInfo('user_id,nick_name,header_img',$user_id);
                        }
                        break;
                }
                return $arr;
            });


//        ' <?php if($row[\'msg_type\'] == \'RC:TxtMsg\')
//                                { echo "<td> 文字</td><td class=\"emojstext\">". $row[\'content\'][\'content\']."</td>";}
/*                                else{ echo \'<td>图片</td><td class="emojstext">\' ."<img src=\\'". $row[\'content\'][\'content\'] ."\\' style=\'max-width: 100px\' > </td>"; } ?>'*/
//


//        dd($rows);
        $this->assign([
            'title' => '聊天记录',
            'rows' => $rows,
            'pageHTML' => $rows->render(),
        ]);

        return view();
    }

}